package com.chlitina.store.modules.cash.model.recharge;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.chlitina.store.common.utils.DataFormater;
import com.chlitina.store.modules.cash.model.CardPayment;
import com.chlitina.store.modules.cash.model.Member;
import com.chlitina.store.modules.cash.model.RechargePayment;

/**
 * This class is used to control which card can be recharged, including new
 * opened cards. When new open a card, user should add it to the card list
 * correctly.
 * 
 * 
 * @author Songsong.Xu
 * 
 */
public class RechargeElementManager {
	private int uuid;// it works for the manager scope
	private List<RehcargeHost> timesCards = null;
	private List<RehcargeHost> durationCards = null;
	private List<RehcargeHost> allCards = null;
	private List<RehcargeHost> productCards = null;
	private RehcargeHost currentElement;

	private List<RehcargeHost> rechargedCardTimes = new ArrayList<RehcargeHost>();
	private List<RehcargeHost> rechargedCardDuration = new ArrayList<RehcargeHost>();
	private List<RehcargeHost> rechargedCardAll = new ArrayList<RehcargeHost>();
	private List<RehcargeHost> rechargedCardProduct = new ArrayList<RehcargeHost>();
	private List<List<String>> paymentDetails=new ArrayList<List<String>>();
	private RechargePayment payment;
	Member member;
	public void generateUUID(RehcargeHost element) {
		element.setUuid(generateUUID());
	}

	public int generateUUID() {
		return ++uuid;
	}

	/**
	 * initialize cards list, it can only initialize once for one member
	 * 
	 * @param member
	 */
	public void init(Member member) {
		this.member=member;
		if (timesCards != null) {
			return;
		}
		timesCards = new ArrayList<RehcargeHost>();
		durationCards = new ArrayList<RehcargeHost>();
		allCards = new ArrayList<RehcargeHost>();
		productCards = new ArrayList<RehcargeHost>();
		for (Object t : member.getTimesCardPayments()) {
			add(timesCards, t);
		}

		for (Object t :  member.getAllCardPayments()) {
			add(allCards, t);
		}

		for (Object t : member.getDurationCardPayments()) {
			add(durationCards, t);
		}

		for (Object t : member.getProductCardPayments()) {
			add(productCards, t);
		}
	}

	public void add(List<RehcargeHost> cards, Object card) {
		cards.add(new RehcargeHost(card, generateUUID()));
	}
	private void remove(List<RehcargeHost> cards, Object card) {
		if(cards==null){
			return;
		}
		List<RehcargeHost> remove=new ArrayList<RehcargeHost>();
		for (RehcargeHost rehcargeHost : cards) {
			if(rehcargeHost.getCard().equals(card)){
				remove.add(rehcargeHost);
			}
		}
		cards.removeAll(remove);
		
	}
	public void removeCard(List<RehcargeHost> list1, List<RehcargeHost> list2,CardPayment cardPayment) {
		 remove(list1, cardPayment);
		 remove(list2, cardPayment);
	}

	
	public RehcargeHost findByUUID(List<RehcargeHost> elements, int uuid) {
		for (RehcargeHost element : elements) {
			if (uuid == element.getUuid()) {
				return element;
			}
		}
		return null;
	}

	public List<RehcargeHost> getTimesCards() {
		return timesCards;
	}

	public List<RehcargeHost> getDurationCards() {
		return durationCards;
	}

	public List<RehcargeHost> getAllCards() {
		return allCards;
	}

	public RehcargeHost getCurrentElement() {
		return currentElement;
	}

	public void setCurrentElement(RehcargeHost currentElement) {
		this.currentElement = currentElement;
	}

	public List<RehcargeHost> getProductCards() {
		return productCards;
	}

	public List<RehcargeHost> getRechargedCardTimes() {
		return rechargedCardTimes;
	}

	public void setRechargedCardTimes(List<RehcargeHost> rechargedCardTimes) {
		this.rechargedCardTimes = rechargedCardTimes;
	}

	public List<RehcargeHost> getRechargedCardDuration() {
		return rechargedCardDuration;
	}

	public void setRechargedCardDuration(
			List<RehcargeHost> rechargedCardDuration) {
		this.rechargedCardDuration = rechargedCardDuration;
	}

	public List<RehcargeHost> getRechargedCardAll() {
		return rechargedCardAll;
	}

	public void setRechargedCardAll(List<RehcargeHost> rechargedCardAll) {
		this.rechargedCardAll = rechargedCardAll;
	}

	public List<RehcargeHost> getRechargedCardProduct() {
		return rechargedCardProduct;
	}

	public void setRechargedCardProduct(List<RehcargeHost> rechargedCardProduct) {
		this.rechargedCardProduct = rechargedCardProduct;
	}

	boolean isEmpty(Collection<?> e) {
		if (e == null) {
			return true;
		}
		return e.isEmpty();
	}



	public void updatePayment() {
		paymentDetails.clear();
		if (payment == null) {
			payment = new RechargePayment();
		}
		 for (RehcargeHost rehcargeHost :  getRechargedCardAll()) {
			 updatePayment(rehcargeHost);
		} 
		 for (RehcargeHost rehcargeHost :  getRechargedCardProduct()) {
			 updatePayment(rehcargeHost);
		} 
		 for (RehcargeHost rehcargeHost :  getRechargedCardDuration()) {
			 updatePayment(rehcargeHost);
		} 
		 for (RehcargeHost rehcargeHost :  getRechargedCardTimes()) {
			 updatePayment(rehcargeHost);
		} 
        payment.update(this);
        
	}

	private void updatePayment(RehcargeHost rehcargeHost) {
		 List<Recharge> recharges = rehcargeHost.getRecharges();
		 if(recharges.isEmpty()){
			 return;
		 }
	     for (Recharge recharge : recharges) {
			recharge.updatePayment(member);
			paymentDetails.add(createRechargeDetail(rehcargeHost,recharge));
		}
	}

	private List<String> createRechargeDetail(RehcargeHost rehcargeHost, Recharge recharge) {
		List<String> list=new ArrayList<String>();
		if(recharge instanceof CardAllRecharge){
			CardAllRecharge eAllRecharge=(CardAllRecharge) recharge;
			list.add("综合卡充值：【"+rehcargeHost.getCardName()+"】 <br/> 充值:"+eAllRecharge.getRechargeMoney()+"元");
			list.add(""+eAllRecharge.getRechargeMoney());
			list.add("1");
			list.add( DataFormater.format(eAllRecharge.getMoney()/eAllRecharge.getRechargeMoney()));
			list.add(""+eAllRecharge.getMoney());
			list.add(""+eAllRecharge.getMoney());
			if(recharge.getPayment().getCardAll()!=null){
				list.add("综合卡：耗卡 "+recharge.getPayment().getCardAll().getRealCost()+ "元");
			}else{
				list.add("现金：付现 "+recharge.getPayment().getCash()+ "元");
			}
		}else if(recharge instanceof CardTimesRecharge){
			CardTimesRecharge r=(CardTimesRecharge) recharge;
			list.add("计次护理卡充值：【"+rehcargeHost.getCardName()+"】 <br/> 充值:"+r.getTimes()+"次");
			list.add(""+r.getMoney());
			list.add("1");
			list.add( DataFormater.format(r.getMoney()/r.getMoney()));
			list.add(""+r.getMoney());
			list.add(""+r.getMoney());
			if(recharge.getPayment().getCardAll()!=null){
				list.add("综合卡：耗卡 "+recharge.getPayment().getCardAll().getRealCost()+ "元");
			}else{
				list.add("现金：付现 "+recharge.getPayment().getCash()+ "元");
			}
		}else if(recharge instanceof CardDurationRecharge){
			CardDurationRecharge r=(CardDurationRecharge) recharge;
			list.add("时段护理卡充值：【"+rehcargeHost.getCardName()+"】 <br/> 充值:"+r.getDuration()+"个月");
			list.add(""+r.getMoney());
			list.add("1");
			list.add( DataFormater.format(r.getMoney()/r.getMoney()));
			list.add(""+r.getMoney());
			list.add(""+r.getMoney());
			if(recharge.getPayment().getCardAll()!=null){
				list.add("综合卡：耗卡 "+recharge.getPayment().getCardAll().getRealCost()+ "元");
			}else{
				list.add("现金：付现 "+recharge.getPayment().getCash()+ "元");
			}
		}else if(recharge instanceof CardProductRecharge){
			CardProductRecharge r=(CardProductRecharge) recharge;
			list.add("产品充值卡充值：【"+rehcargeHost.getCardName()+"】 <br/> 充值:"+r.getRechargeMoney()+"元");
			list.add(""+r.getMoney());
			list.add("1");
			list.add( DataFormater.format(r.getMoney()/r.getMoney()));
			list.add(""+r.getMoney());
			list.add(""+r.getMoney());
			if(recharge.getPayment().getCardAll()!=null){
				list.add("综合卡：耗卡 "+recharge.getPayment().getCardAll().getRealCost()+ "元");
			}else{
				list.add("现金：付现 "+recharge.getPayment().getCash()+ "元");
			}
		}
		return list;
	}

	public boolean isEmpty() {
		if (!isEmpty(rechargedCardAll)) {
			return false;
		}
		if (!isEmpty(rechargedCardProduct)) {
			return false;
		}
		if (!isEmpty(rechargedCardTimes)) {
			return false;
		}
		if (!isEmpty(rechargedCardDuration)) {
			return false;
		}
		return true;
	}

	public RechargePayment getPayment() {
		return payment;
	}

	public void setPayment(RechargePayment payment) {
		this.payment = payment;
	}

	public List<List<String>> getPaymentDetails() {
		return paymentDetails;
	}

	

}
